package com.zs.members.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gsj.common.pojo.R;
import com.zs.members.pojo.entity.Coupon;
import com.zs.members.dao.CouponMapper;
import com.zs.members.pojo.vo.CouponVo;
import com.zs.members.service.CouponService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 张硕
 * @since 2021-07-25
 */
@Service
public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> implements CouponService {

    @Autowired
    private CouponMapper couponMapper;

    @Override
    public R get(Integer currentPage, Integer pageSize, CouponVo couponVo) {
        Page<Coupon> page = new Page<>(currentPage,pageSize);
        QueryWrapper<Coupon> wrapper = new QueryWrapper<>();
        if (couponVo.getId()!=null){
            wrapper.like("id",couponVo.getId());
        }
        if (couponVo.getShopId()!=null){
            wrapper.like("shop_id",couponVo.getShopId());
        }
        if (couponVo.getType()!=null){
            wrapper.like("type",couponVo.getType());
        }
        if (StringUtils.isNotEmpty(couponVo.getName())){
            wrapper.like("name",couponVo.getName());
        }
        if (couponVo.getPlatform()!=null){
            wrapper.like("platform",couponVo.getPlatform());
        }
        if (StringUtils.isNotEmpty(couponVo.getStartTime())){
            wrapper.like("start_time",couponVo.getStartTime());
        }
        if (StringUtils.isNotEmpty(couponVo.getEndTime())){
            wrapper.like("end_time",couponVo.getEndTime());
        }
        if (couponVo.getUseType()!=null){
            wrapper.like("use_type",couponVo.getUseType());
        }
        if (StringUtils.isNotEmpty(couponVo.getEnableTime())){
            wrapper.like("enable_time",couponVo.getEnableTime());
        }
        Page<Coupon> couponPage = couponMapper.selectPage(page, wrapper);
        return R.ok().put("records",couponPage.getRecords()).put("total",couponPage.getTotal());
    }
}
